package model.xml;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.io.Reader;
import java.io.Writer;
import java.sql.SQLException;
import java.sql.SQLXML;

import javax.xml.transform.Result;
import javax.xml.transform.Source;


import model.address.Address;

import org.simpleframework.xml.Serializer;
import org.simpleframework.xml.core.Persister;


public class SerializeObject {

	
	Address example = new Address();
	

	public static String serializeToString(Object object) throws Exception {
		Serializer serializer = new Persister(); 			//shamanism
		OutputStream result = new ByteArrayOutputStream();	//Need For Stream
		serializer.write(object, result);					//shamanism create XML from object and put it to stream. Powerfull magic
		return ((ByteArrayOutputStream) result).toString(); //Mahalai bahalai stream to string prevrashai
	}
	
	public static OutputStream serializeToOutputStream(Object object) throws Exception {
		Serializer serializer = new Persister();
		OutputStream result = new ByteArrayOutputStream();
		serializer.write(object, result);
		return result;
	}
	
	public static Object deSerializeFromXml(Class pClass, SQLXML sqlxml) throws Exception {
		Serializer serializer = new Persister();
		//System.out.println(sqlxml.getString()); //view deserialisable xml
		InputStream binaryStream = sqlxml.getBinaryStream();
		return serializer.read(pClass, binaryStream);
	}
	
	public static Object deSerializeFromString(Class pClass, String string) throws Exception {
		Serializer serializer = new Persister();
		return serializer.read(pClass, string);
	}
	
}
